<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'weight_type_text',
        'createtime_text',
        'deliverytime_text',
        'receivingtime_text',
        'scheduledtime_text',
        'accepttime_text',
        'successtime_text'
    ];
    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '6' => __('Status 6')];
    }

    public function getWeightTypeList()
    {
        return ['1' => __('Weight_type 1'), '2' => __('Weight_type 2'), '3' => __('Weight_type 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWeightTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['weight_type']) ? $data['weight_type'] : '');
        $list = $this->getWeightTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getDeliverytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['deliverytime']) ? $data['deliverytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getReceivingtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['receivingtime']) ? $data['receivingtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getScheduledtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['scheduledtime']) ? $data['scheduledtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getAccepttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['accepttime']) ? $data['accepttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSuccesstimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['successtime']) ? $data['successtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setScheduledtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAccepttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSuccesstimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function region()
    {
        return $this->belongsTo('Region', 'region_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function details()
    {
        return $this->hasMany('OrderDetail', 'order_id', 'id');
    }

    public function appraise()
    {
        return $this->belongsTo('OrderAppraise', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
    }
}
